import { Component, OnInit } from '@angular/core';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import { NzMessageService } from 'ng-zorro-antd';
import { FetchService } from '../../../../core/net/fetch.service';
import * as moment from 'moment';

@Component({
  selector: 'app-dr-flood-prevention',
  templateUrl: './dr-flood-prevention.component.html',
  styleUrls: ['./dr-flood-prevention.component.scss', '../duty-records.component.scss']
})
export class DrFloodPreventionComponent implements OnInit {
    searchForm: FormGroup;
    tableData: Array<any> = [];
    pageNum = 1;
    pageSize = 10;
    total: number;
    loading = false;
    form: FormGroup;
    modalTitle: string = '';
    modalShow: boolean = false;

    constructor(private fb: FormBuilder,
                private message: NzMessageService,
                private fetchService: FetchService) {
        this.searchForm = this.fb.group({
            ctCreatetime: [],
        });
        this.form = this.fb.group({
            id: [null],
            ctCreatetime: [new Date(), [Validators.required]],
            info: [null, [Validators.required]],
            processInfo: [null, [Validators.required]],
            leaderInstructions: [null, [Validators.required]],
            onDutyDispatch: [null, [Validators.required]],
            onDutyManager: [null, [Validators.required]],
            onDutyLeader : [null, [Validators.required]]
        });
    }

    ngOnInit(): void {
        this.queryInfo();
    }

    queryInfo(reset: boolean = false): void {
        if (reset) {
            this.pageNum = 1;
        }
        this.loading = true;
        const params = {
            ctCreatetime: this.searchForm.value.ctCreatetime ? moment(this.searchForm.value.ctCreatetime).format('YYYY-MM-DD') : null,
            pageSize: this.pageSize,
            pageNum: this.pageNum
        };
        this.fetchService.post('schedule/yjqFloodControlDuty/list', params).subscribe(res => {
            this.tableData = res['list'];
            this.total = res['total'];
            this.loading = false;
        });
    }

    resetQueryInfo(): void {
      this.searchForm.reset();
      this.queryInfo(true);
    }

    openEditModal(item?): void {
        this.modalTitle = item ? `防汛值班详情` : '新增值班记录';
        this.form.reset(item || {});
        this.modalShow = true;
    }

    onDelete(id) {
        this.fetchService.get(`schedule/yjqFloodControlDuty/remove/${id}`).subscribe(resp => {
            this.message.info('删除成功');
            this.queryInfo();
        });
    }

    onConfirm() {
        if (!this.form.valid) {
            this.message.warning('必填信息不能为空！');
            return;
        }
        const data = this.form.value;
        const url = data.id ? 'schedule/yjqFloodControlDuty/edit' : 'schedule/yjqFloodControlDuty/add';
        this.fetchService.post(url, data).subscribe(res => {
            this.message.success('提交成功');
            this.modalShow = false;
            this.queryInfo();
            this.form.reset();
        });
    }

    onCancel() {
        this.form.reset();
        this.modalShow = false;
    }
}

